{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook we will see problems caused by latent variables. We start by learning a BN from a dataset called \"latent.dat\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using license file /home/james/gurobi.lic\n",
      "Academic license - for non-commercial use only\n",
      "Changed value of parameter PreCrush to 1\n",
      "   Prev: 0  Min: 0  Max: 1  Default: 0\n",
      "Changed value of parameter CutPasses to 100000\n",
      "   Prev: -1  Min: -1  Max: 2000000000  Default: -1\n",
      "Changed value of parameter GomoryPasses to 100000\n",
      "   Prev: -1  Min: -1  Max: 2000000000  Default: -1\n",
      "Changed value of parameter MIPFocus to 2\n",
      "   Prev: 0  Min: 0  Max: 3  Default: 0\n",
      "Changed value of parameter ZeroHalfCuts to 2\n",
      "   Prev: -1  Min: -1  Max: 2  Default: -1\n",
      "Changed value of parameter MIPGap to 0.0\n",
      "   Prev: 0.0001  Min: 0.0  Max: inf  Default: 0.0001\n",
      "Changed value of parameter MIPGapAbs to 0.0\n",
      "   Prev: 1e-10  Min: 0.0  Max: inf  Default: 1e-10\n",
      "**********\n",
      "BN has score -47095.73985251983\n",
      "**********\n",
      "A<- -10995.31277923398\n",
      "B<-A,E -7147.3928675571005\n",
      "E<- -10911.8504220803\n",
      "C<-D,E -7055.36961708474\n",
      "D<- -10985.814166563709\n",
      "**********\n",
      "bnlearn modelstring = \n",
      "[A][B|E:A][E][C|D:E][D]\n",
      "**********\n",
      "CPDAG:\n",
      "Vertices: A,B,E,C,D\n",
      "A->B\n",
      "E->B\n",
      "E->C\n",
      "D->C\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW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N27d0dAQAAyMzNRq1YtrFq1SrskItM7ffo0goODEcDzFYrElW4hUlJSkJ6ejpycHMyePRt33303jh07pl0WkandfPPNOHXqFPLy8rRLMTWGbhH8/f3Ro0cP+Pv78+kr0Q3ceeedqFixIlJSUrRLMTWGbhHcbjeWLVuGs2fPonnz5trlEJla1apVMWHCBPTv3x8pKSnIyspCbm4uVq5cieHDh2uXZxpsvhSia9eu8Pf3h8vlQmhoKBYsWIDw8HDtsohMLyoqCrVr10ZcXBx69+6NoKAgtGvXDjExMdqlmQbPXiAi8jKevUBEZBIMXSIiAzF0iYgMxNAlIjIQQ5eIyEAMXSIiAzF0iYgMxNAlIjIQQ5eIyEAMXSIiAzF0iYgMxNAlIjIQQ5eIyEAMXSIiAxV5tKPL5ToJYL9x5RAR2UKo2+2uWdhvFBm6RETkXWwvEBEZiKFLRGQghi4RkYEYukREBmLoEhEZ6P8BlhYmL1tNuTkAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pygobnilp.gobnilp import Gobnilp\n",
    "m = Gobnilp()\n",
    "m.learn(\"latent.dat\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In fact the data \"latent.dat\" is synthetic data generated from a BN with the same structure as the one just learned. So we have successfully learned the 'true' network. It is useful to look at the local scores for each of the five variables. (These are 'BDeu' local scores with the equivalent sample size parameter set to 1.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "\t frozenset() -10995.31277923398\n",
      "\t frozenset({'B'}) -9261.366308775898\n",
      "\t frozenset({'E', 'B'}) -9031.158112153433\n",
      "B\n",
      "\t frozenset() -9739.922761872192\n",
      "\t frozenset({'A'}) -8005.97629141411\n",
      "\t frozenset({'E'}) -9132.864764306818\n",
      "\t frozenset({'E', 'A'}) -7147.3928675571005\n",
      "C\n",
      "\t frozenset() -7119.059423788\n",
      "\t frozenset({'D'}) -7078.349701120504\n",
      "\t frozenset({'D', 'E'}) -7055.36961708474\n",
      "D\n",
      "\t frozenset() -10985.814166563709\n",
      "\t frozenset({'C'}) -10945.104443896213\n",
      "\t frozenset({'E', 'C'}) -10925.541722470429\n",
      "E\n",
      "\t frozenset() -10911.8504220803\n",
      "\t frozenset({'B'}) -10304.792424514926\n",
      "\t frozenset({'A', 'B'}) -10074.584227892461\n",
      "\t frozenset({'D', 'C'}) -10908.350883139276\n"
     ]
    }
   ],
   "source": [
    "for child, scored_parentsets in m.local_scores.items():\n",
    "    print(child)\n",
    "    for pas, score in scored_parentsets.items():\n",
    "        print('\\t',pas,score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The key variable is B. The local score for B having both A and E as parents is much better than any other options. For C having both D and E as parents is a little better than D alone. For A, C and E, having no parent is not that much worse than the other options. (Note that many possible parent sets have been 'pruned' away.) \n",
    "As a result we get the 'W' shaped BN where B and C are **not** independent.\n",
    "Consider the conditional independence relations between variables A, B, C and D (ignoring E) represented by the learned BN. \n",
    "1. $A \\perp C$\n",
    "2. $A \\perp D$\n",
    "3. $B \\perp D$\n",
    "4. $A \\perp D | B$\n",
    "5. $A \\perp D | C$\n",
    "6. $B \\perp D | A$\n",
    "\n",
    "None of the 543 DAGs on 4 variables have these and only these 6 conditional independence relations. So what happens when we learn using just the variables A,B,C and D, so that E becomes a *latent* variable? Let's see."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**********\n",
      "BN has score -37065.4529383323\n",
      "**********\n",
      "A<-B -9261.366308775898\n",
      "B<- -9739.922761872192\n",
      "C<-D -7078.349701120504\n",
      "D<- -10985.814166563709\n",
      "**********\n",
      "bnlearn modelstring = \n",
      "[A|B][B][C|D][D]\n",
      "**********\n",
      "CPDAG:\n",
      "Vertices: A,B,C,D\n",
      "B-A\n",
      "D-C\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAI5klEQVR4nO3dQYhVdR/H4d+dexVnmlBzJs1sHAgKiyhoSIvCFhGkGSYaFkFEmzCDmEXUQBsJoQhp4cJdXCHFVjOOEG5zpIUKBqEWBI4lak1lzTDORe2+i9A3wVy8vf7OubfngVnoWcyXWXz8z7mHY6XZbAYAOTqKHgDwbyK6AIlEFyCR6AIkEl2ARLUbXezp6Wn29/cnTQFoD0eOHJloNpu917t2w+j29/fH4cOHb84qgDZVqVTG/+6a2wsAiUQXIJHoAiQSXYBEoguQSHQBEokuQCLRBUgkugCJRBcgkegCJBJdgESiC5CoFNHt7++Pzs7O6O7ujvnz58fq1avj+++/L3oW0IKu9OTWW2+NefPmxWOPPRY7duyIP/74o+hpEVGS6EZEjI6OxtTUVJw5cyYWLlwYb775ZtGTgBY1Ojoak5OTMT4+Hu+880588MEH8dprrxU9KyJKFN0r5syZE+vXr49jx44VPQVocXPnzo3nnnsu9uzZE/V6Pb7++uuiJ5UvutPT07Fnz55YsWJF0VOANvHII4/EkiVL4sCBA0VPufH/HJFp7dq1UavVYmpqKm6//fbYv39/0ZOANrJ48eL45Zdfip5RnpPu8PBwnD9/PhqNRmzfvj1WrlwZZ8+eLXoW0CZOnz4dt912W9EzyhPdK6rVaqxbty6q1WqMjY0VPQdoA4cOHYrTp0/H448/XvSU8kW32WzGyMhI/Prrr7Fs2bKi5wAt7Pfff499+/bFxo0b4+WXX44HHnig6Enluae7Zs2aqFarUalUYunSpVGv1+P+++8vehbQgtasWRO1Wi06Ojrivvvui8HBwXj99de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VagwNDRU9D2gxr7zySnR3d8fk5GRcvHgxGo1GTE5ORl9fX6xataroecVHNyJi69atMWvWrKt/rlarsWrVqrj33nsLXAW0os7Oznjvvfeiq6vr6t91d3fHhx9+GB0dxSev+AURcc8998Tq1auv/kBmzZoVW7duLXgV0Ko2bdp0zUFuwYIFsX79+gIX/Vcpohvx52m3Vvvzcz2nXOCfuHLardVqMXv27NKcciNK8EHaX61cuTK++OKLOHHihOgC/8iFCxdi3rx5ccstt8TExERqdG/0QVrhj4z91UcffRQff/yx4AL/WGdnZ7z77ruxePHi0pxyI0p20gVoB6V+ZAzg30R0ARKJLkCiUkX3ySefjPnz50ej0Sh6CtDidu3aFQMDA9Hd3R133HFHPPPMMzE2Nlb0rPJE9+TJk3HgwIGoVCqxd+/eoucALWzbtm3x1ltvxdDQUJw7dy5OnToVmzZtipGRkaKnlefphS1btsT+/ftj+fLl8e2338a+fftSvi/QXn777be4884745NPPokNGzYUsqElntPduXNnDA4OxvLly2PFihVx7ty5WLhwYdGzgBbz5ZdfxszMTDz//PNFT7muUtxeGBsbi/Hx8XjhhRfi4Ycfjrvvvjt27dpV9CygBf3888/R09Nz9bUCZVOK6Nbr9Xj66aejp6cnIiJeeukl79EF/icLFiyIiYmJuHTpUtFTrqvwfwouXLgQn332WVy+fDkWLVoUERGNRiPOnz8fX331VTz44IMFLwRayaOPPhpz5syJ4eHh0rxZ7K8KP+kODw9HtVqNY8eOxdGjR+Po0aNx/PjxeOKJJ2Lnzp1FzwNazNy5c2PLli3xxhtvxPDwcExPT8fFixfj888/j7fffrvoecVHt16vx6uvvhp9fX2xaNGiq1+bN2+OTz/9tLS/IgDlNTg4GNu2bYv3338/ent746677ort27fH2rVri55WnkfGANqFF94AlIToAiQSXYBEoguQSHQBEokuQCLRBUgkugCJRBcgkegCJBJdgESiC5BIdAESiS5Aohu+2rFSqfwUEeN5cwDawtJms9l7vQs3jC4A/19uLwAkEl2ARKILkEh0ARKJLkCi/wCDkeY08iQ/RAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m.set_bn_variables('ABCD')\n",
    "m.learn(start='data',palim=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We end up with a 4 node BN where $B \\perp C$, even though when E was present we learned a BN where $B \\not \\perp C$. This just basically shows the limitations of Gobnilp's form of learning when there are latent variables.\n",
    "\n",
    "The remainder of this notebook just shows the results of learning on various subsets of the original variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**********\n",
      "BN has score -26120.34849443609\n",
      "**********\n",
      "A<- -10995.31277923398\n",
      "B<-A -8005.97629141411\n",
      "C<- -7119.059423788\n",
      "**********\n",
      "bnlearn modelstring = \n",
      "[A][B|A][C]\n",
      "**********\n",
      "CPDAG:\n",
      "Vertices: A,B,C\n",
      "A-B\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAHYklEQVR4nO3dT4iX1R7H8e/PGWOigRwa/0SoE62kRVADjmDYpiAiMDEX7YRWVqAiLqJN0rZZzT5GUKnVqIEMLRuYjYJtDHKjtki5ior6q/FPz93FvaDCdTyfO1OvF8zqwPk+qzeHw/P8ptd1XQGQseL//QAA/ySiCxAkugBBogsQJLoAQYOPWxwdHe3GxsZCjwLw93DmzJmrXdetftjaY6M7NjZWp0+fbvNUAH9TvV7v4qPWXC8ABIkuQJDoAgSJLkCQ6AIEiS5AkOgCBIkuQJDoAgSJLkCQ6AIEiS5AkOgCBDWP7ltvvVUjIyO1sLDQehTAoh09erTGx8dreHi4XnzxxXr33Xdrbm7uqe3fNLoXLlyoH3/8sXq9Xp04caLlKIBFm5ycrL1799bnn39eV65cqUuXLtWePXvq+PHjT21G73H/gn18fLxbzO/pHjp0qGZnZ2vz5s31yy+/1Pfff//EewG0dPPmzXrppZfqm2++qQ8//HBRe/V6vTNd140/bO2xP2K+WIcPH679+/fX5s2ba2Jioq5cuVJr165tORLgiczPz9cff/xRH3zwQdM5za4X5ubm6uLFi7Vr165644036pVXXqmjR4+2GgewKNeuXavR0dEaHGx6Fm0X3enp6XrnnXdqdHS0qqo++uijmp6ebjUOYFFeeOGFunr1at2/f7/pnCZJ//333+u7776rBw8e1Lp166qqamFhoW7cuFE//fRTvfbaay3GAjyxLVu21NDQUM3MzNTOnTubzWly0p2ZmamBgYE6d+5cnT17ts6ePVs///xzvfnmm3X48OEWIwEW5fnnn69Dhw7VJ598UjMzM9Xv9+vevXt16tSpOnjw4FOb0yS609PTtXv37tqwYUOtW7fur79PP/20jhw50vz4DvAk9u/fX5OTk/XVV1/V6tWra/369TU1NVXbt29/ajOavjIG8E/0uFfGfAYMECS6AEGiCxDUPLp37typH374ofUYgGWheXSvX79eu3fvbj0GYFlwvQAQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQYMtNv3zzz/r5MmT1e/369q1a9Xv9+vYsWNVVbV+/fraunVri7EAS16v67pHLo6Pj3enT5/+nze9e/dujYyMVNd1tWLFiur3+zU8PFwLCwv1+uuv1/z8/GKeGWBJ6/V6Z7quG3/YWpPrhWeeeab27dtXVVV37typruvq1q1btXLlyvryyy9bjARYFprd6R44cKBWrPjv7V9++eV6++23W40EWPKaRXfVqlW1d+/eGhoaqqqq5557rr7++uvq9XqtRgIseU3fXjhw4MBfkXXKBWgc3VWrVtXHH39cVeWUC1CNXhn7T1988UWdP3/eKRegAtFds2ZNnTp1qvUYgGXBF2kAQaILECS6AEHNojs2NlbPPvtsDQ8P18jISL333nv166+/thoHsCw0PemePHmybt++Xb/99lutXbu2Pvvss5bjAJa8yPXC0NBQ7dy5s86dO5cYB7BkRaLb7/fr22+/rYmJicQ4gCWr6Xu627dvr8HBwbp9+3atWbOmZmdnW44DWPKannRnZmbqxo0btbCwUFNTU7Vt27a6fPlyy5EAS1rkemFgYKB27NhRAwMDNTc3lxgJsCQ1/wy4qqrrujpx4kRdv369Nm3alBgJsCQ1je77779fAwMD1ev1auPGjTU9PV2vvvpqy5EAS1qz6F64cKHV1gDLls+AAYJEFyBIdAGCRBcgSHQBgkQXIEh0AYJEFyBIdAGCRBcgSHQBgkQXIEh0AYJEFyCo13Xdoxd7vX9V1cXc4wD8LWzsum71wxYeG10Ani7XCwBBogsQJLoAQaILECS6AEH/BgkgVfoihpLWAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m.set_bn_variables('ABC')\n",
    "m.learn(start='data')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**********\n",
      "BN has score -27804.086629556405\n",
      "**********\n",
      "B<- -9739.922761872192\n",
      "C<- -7119.059423788\n",
      "D<-C -10945.104443896213\n",
      "**********\n",
      "bnlearn modelstring = \n",
      "[B][C][D|C]\n",
      "**********\n",
      "CPDAG:\n",
      "Vertices: B,C,D\n",
      "C-D\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAHCElEQVR4nO3dQWjX9R/H8fdv+w02plhpDlvg7yZLOjlkiSAeRRc1ggK96SGKQIZEFMgQLyEEgQMRL7sIu20oiBcPFUSUlyi7ZiUWf8vU+dMo+3X48/8ToULm9xW4x+O4L3xf39OTL5/tt1+r1+sVABl9//YDACwnogsQJLoAQaILECS6AEHt+11cs2ZNr9PphB4F4NFw/vz5K71e78m7XbtvdDudTn322WfNPBXAI6rVal281zXHCwBBogsQJLoAQaILECS6AEGiCxAkugBBogsQJLoAQaILECS6AEGiCxAkugBBDz26nU6nhoaGasWKFfX444/Xzp0769tvv33YMwAPzcmTJ2t8fLxWrFhR69atqx07dtRHH33UyFYjb7qnTp2qpaWlunz5co2MjNQbb7zRxAzAP/bee+/V/v376+23364ffvihvvnmm3rttddqcXGxkb1GjxcGBwfrpZdeqgsXLjQ5A/BArl27VgcPHqzZ2dmampqq4eHhGhgYqMnJyTpy5Egjm41Gt9vt1vz8fE1MTDQ5A/BAPv7447p9+3a9+OKL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BIdAGCRBcgSHQBgkQXIEh0AYJEFyBIdAGCRBcgSHQBgkQXIKj9bz8AwL/lyy+/rM8//7yqqr777rv64IMPqtvtVl9fX+3atauGh4cf+qboAsvWiRMnanZ2tgYHB6vb7dZXX31VfX19dfPmzbpw4UKNjY099E3HC8CydeDAgWq323Xjxo26c+dO3bp1q7rdbm3fvr2R4FaJLrCMjY6O1u7du2tgYOD/PxscHKwjR440tim6wLI2MzNT7fZ/T1pbrVZNTEzUpk2bGtsTXWBZ+9/bbqvVqoGBgUbfcqtEF6BmZmaqqmrDhg2NvuVW+esFgBodHa19+/bVyy+/3PiW6AJU1fHjxyM7jhcAgkQXIEh0AYJEF1i2Op1ODQ0N1cqVK+uxxx6rLVu21LFjx+r3339vbFN0gWXt1KlTdePGjbp48WK99dZb9e6779bevXsb2xNdgKpatWpVPf/88zU/P19zc3P1xRdfNLIjugB/snnz5nr66afrww8/bOT+ogvwF0899VT99NNPjdxbdAH+4tKlS/XEE080cm/RBfiTTz/9tC5dulRbt25t5P6iC1BV169fr9OnT9crr7xSe/bsqWeffbaRHf97AVjWJicnq91uV19fXz3zzDM1PT1dr776amN7ogssW19//XV80/ECQJDoAgSJLkCQ6AIEiS5AkOgCBIkuQJDoAgSJLkCQ6AIEiS5AkOgCBIkuQJDoAgS1er3evS+2Wv+pqou5xwF4JKzv9XpP3u3CfaMLwMPleAEgSHQBgkQXIEh0AYJEFyDoD9WOHjqBLEN3AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m.set_bn_variables('BCD')\n",
    "m.learn(start='data')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**********\n",
      "BN has score -16858.982185660192\n",
      "**********\n",
      "B<- -9739.922761872192\n",
      "C<- -7119.059423788\n",
      "**********\n",
      "bnlearn modelstring = \n",
      "[B][C]\n",
      "**********\n",
      "CPDAG:\n",
      "Vertices: B,C\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "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\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m.set_bn_variables('BC')\n",
    "m.learn(start='data')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
